package cn.iocoder.yudao.module.erp.controller.admin.supplierfleet;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE;

import cn.iocoder.yudao.module.erp.controller.admin.supplierfleet.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.supplierfleet.SupplierFleetDO;
import cn.iocoder.yudao.module.erp.service.supplierfleet.SupplierFleetService;

@Tag(name = "管理后台 - 供应商车队信息")
@RestController
@RequestMapping("/erp/supplier-fleet")
@Validated
public class SupplierFleetController {

    @Resource
    private SupplierFleetService supplierFleetService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商车队信息")
//    @PreAuthorize("@ss.hasPermission('erp:supplier-fleet:create')")
    public CommonResult<Integer> createSupplierFleet(@Valid @RequestBody SupplierFleetSaveReqVO createReqVO) {
        return success(supplierFleetService.createSupplierFleet(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商车队信息")
//    @PreAuthorize("@ss.hasPermission('erp:supplier-fleet:update')")
    public CommonResult<Boolean> updateSupplierFleet(@Valid @RequestBody SupplierFleetSaveReqVO updateReqVO) {
        supplierFleetService.updateSupplierFleet(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商车队信息")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:supplier-fleet:delete')")
    public CommonResult<Boolean> deleteSupplierFleet(@RequestParam("id") Integer id) {
        supplierFleetService.deleteSupplierFleet(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商车队信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:supplier-fleet:query')")
    public CommonResult<SupplierFleetRespVO> getSupplierFleet(@RequestParam("id") Integer id) {
        SupplierFleetDO supplierFleet = supplierFleetService.getSupplierFleet(id);
        return success(BeanUtils.toBean(supplierFleet, SupplierFleetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商车队信息分页")
//    @PreAuthorize("@ss.hasPermission('erp:supplier-fleet:query')")
    public CommonResult<PageResult<SupplierFleetRespVO>> getSupplierFleetPage(@Valid SupplierFleetPageReqVO pageReqVO) {
        PageResult<SupplierFleetDO> pageResult = supplierFleetService.getSupplierFleetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplierFleetRespVO.class));
    }

    public List<SupplierFleetDO> formatCurrency(List<SupplierFleetDO> list){
        for (SupplierFleetDO supplierFleetDO : list) {
            if (supplierFleetDO.getCurrencyType()==1) {
                supplierFleetDO.setCurrencyName("￥人民币");
            }else if (supplierFleetDO.getCurrencyType()==2) {
                supplierFleetDO.setCurrencyName("$美元");
            }else if (supplierFleetDO.getCurrencyType()==3) {
                supplierFleetDO.setCurrencyName("KZT坚戈");
            }else {
                supplierFleetDO.setCurrencyName("₽卢布");
            }
        }
        return list;
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商车队信息 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:supplier-fleet:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSupplierFleetExcel(@Valid SupplierFleetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PAGE_SIZE_NONE);
        List<SupplierFleetDO> list = supplierFleetService.supplierList(pageReqVO.getName(),pageReqVO.getIsShow());

        // 导出 Excel
        ExcelUtils.write(response, "供应商信息.xls", "数据", SupplierFleetRespVO.class,
                        BeanUtils.toBean(formatCurrency(list), SupplierFleetRespVO.class));
    }

    @GetMapping("/export-excel1")
    @Operation(summary = "导出车队信息 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:supplier-fleet:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSupplierFleetExcel1(@Valid SupplierFleetPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PAGE_SIZE_NONE);
        List<SupplierFleetDO> list = supplierFleetService.fleetList(pageReqVO.getName(),pageReqVO.getFleet());

        // 导出 Excel
        ExcelUtils.write(response, "车队信息.xls", "数据", SupplierFleetRespVO.class,
                BeanUtils.toBean(formatCurrency(list), SupplierFleetRespVO.class));
    }

    @GetMapping("/export-excel2")
    @Operation(summary = "导出支付信息 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:supplier-fleet:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSupplierFleetExcel2(@Valid SupplierFleetPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PAGE_SIZE_NONE);
        List<SupplierFleetDO> list = supplierFleetService.fleetPayList(pageReqVO.getName(),pageReqVO.getFleet());
        for (SupplierFleetDO supplierFleetDO : list){
            if (supplierFleetDO.getCurrencyType()==1) {
                supplierFleetDO.setCurrencyName("￥人民币");
            }else if (supplierFleetDO.getCurrencyType()==2) {
                supplierFleetDO.setCurrencyName("$美元");
            }else if (supplierFleetDO.getCurrencyType()==3) {
                supplierFleetDO.setCurrencyName("KZT坚戈");
            }else {
                supplierFleetDO.setCurrencyName("₽卢布");
            }
            JSONObject jsonObject=JSONObject.parseObject(supplierFleetDO.getInfo());
            supplierFleetDO.setDepartment((String) jsonObject.get("department"));
            supplierFleetDO.setRecordDate((String) jsonObject.get("recordDate"));
            supplierFleetDO.setRemark((String) jsonObject.get("remark"));
            supplierFleetDO.setDispatchNumber((String) jsonObject.get("dispatchNumber"));
            supplierFleetDO.setWeightTotal((jsonObject.getDoubleValue("weightTotal")));
            supplierFleetDO.setVolumeTotal((jsonObject.getDoubleValue("volumeTotal")));
        }
        // 导出 Excel
        ExcelUtils.write(response, "车队支付信息.xls", "数据", SupplierFleetRespVO.class,
                BeanUtils.toBean(list, SupplierFleetRespVO.class));
    }

    @GetMapping("/supplierList")
    @Operation(summary = "供应商列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp:detail')")
    public CommonResult<List<SupplierFleetDO>> supplierList(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "isShow",required = false) Integer isShow) {
        return success(supplierFleetService.supplierList(name,isShow));
    }

    @GetMapping("/bankFleetList")
    @Operation(summary = "银行卡和供应商列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp:detail')")
    public CommonResult<List<Object>> bankFleetList(@RequestParam(value = "name",required = false) String name,@RequestParam(value = "fleet",required = false) String fleet) {
        return success(supplierFleetService.bankFleetList(name,fleet));
    }

    @GetMapping("/fleetList")
    @Operation(summary = "车队列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp:detail')")
    public CommonResult<List<SupplierFleetDO>> fleetList(@RequestParam(value = "name",required = false) String name,@RequestParam(value = "fleet",required = false) String fleet) {
        return success(supplierFleetService.fleetList(name,fleet));
    }

    @GetMapping("/fleetPayList")
    @Operation(summary = "车队支付明细")
//    @PreAuthorize("@ss.hasPermission('erp:dp:detail')")
    public CommonResult<List<SupplierFleetDO>> fleetPayList(@RequestParam("name") String name,@RequestParam("fleet") String fleet) {
        return success(supplierFleetService.fleetPayList(name,fleet));
    }

    @GetMapping("/totalMoney")
    @Operation(summary = "车队金额总计")
//    @PreAuthorize("@ss.hasPermission('erp:dp:detail')")
    public CommonResult<List<SupplierFleetDO>> totalMoney(@RequestParam(value = "name",required = false) String name,@RequestParam(value = "fleet",required = false) String fleet,@RequestParam(value = "isShow",required = false) Integer isShow) {
        return success(supplierFleetService.totalMoney(name,fleet,isShow));
    }
}